package com.fast.admin.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fast.admin.entity.sys.RoleResource;
import com.fast.admin.mapper.sys.IRoleResourceMapper;
import com.fast.admin.service.sys.IRoleResourceService;
import com.fast.common.annotation.Commit;
import com.fast.common.base.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title：角色资源service
 * @author：ylc
 * @date：2018/8/6 16:34
 * @version：1.0
 **/
@Service
public class RoleResourceService extends BaseService<IRoleResourceMapper, RoleResource> implements IRoleResourceService {

    @Override
    public List<RoleResource> selectByRole(Long roleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", roleId);
        return super.selectList(wrapper);
    }

    @Override
    @Commit
    public void insertBatchByRole(Long roleId, String resourceId) {
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("role_id", roleId);
        super.delete(wrapper);
        List<RoleResource> roleResources = Arrays.asList(resourceId.split(",")).stream().map(c -> new RoleResource(roleId, Long.valueOf(c))).collect(Collectors.toList());
        super.insertBatch(roleResources, roleResources.size());
    }
}
